﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DotNetCore.CAP;
using Microsoft.AspNetCore.Mvc;
using MySql.Data.MySqlClient;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace NETCORE.CAP.Controllers
{
    [Route("api/[controller]")]
    public class PublishController : Controller
    {

        private readonly ICapPublisher _capBus;

        public PublishController(ICapPublisher capPublisher)
        {
            _capBus = capPublisher;
        }


        /// <summary>
        /// 不使用事务
        /// </summary>
        /// <returns></returns>
        [Route("~/without/transaction")]
        public IActionResult WithoutTransaction()
        {
            _capBus.Publish("xxx.services.show.time", DateTime.Now);

            return Ok();
        }


        ////Ado.Net 中使用事务，自动提交
        //[Route("~/adonet/transaction")]
        //public IActionResult AdonetWithTransaction()
        //{
        //    using (var connection = new MySqlConnection(ConnectionString))
        //    {
        //        using (var transaction = connection.BeginTransaction(_capBus, autoCommit: true))
        //        {
        //            //业务代码

        //            _capBus.Publish("xxx.services.show.time", DateTime.Now);
        //        }
        //    }
        //    return Ok();
        //}

        ////EntityFramework 中使用事务，自动提交
        //[Route("~/ef/transaction")]
        //public IActionResult EntityFrameworkWithTransaction([FromServices]AppDbContext dbContext)
        //{
        //    using (var trans = dbContext.Database.BeginTransaction(_capBus, autoCommit: true))
        //    {
        //        //业务代码

        //        _capBus.Publish("xxx.services.show.time", DateTime.Now);
        //    }
        //    return Ok();
        //}
         
    }
}
